<?php
/**
*
* @package ppkBB3cker
* @version $Id: tvalidip.php 1.000 2009-08-13 15:12:00 PPK $
* @copyright (c) 2009 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

define('TRACKER_ZONES_NAME', $table_prefix . 'tracker_zname');
define('TRACKER_ZONES_ADDR', $table_prefix . 'tracker_zaddr');

$reserved_ips=t_getcache('blockedip', 'reserved_ips');

if($reserved_ips===FALSE)
{
	include_once($tincludedir.'tcache.'.$phpEx);
	$sql="SELECT INET_NTOA(a.addr_start), INET_NTOA(a.addr_end) FROM ".TRACKER_ZONES_NAME." n, ".TRACKER_ZONES_ADDR." a WHERE n.zone_type='r' AND n.id=a.zone_id";
	$result=my_sql_query($sql);
	while($row=mysql_fetch_row($result))
	{
		$reserved_ips[]=array($row[0], $row[1]);
	}
	t_recache('blockedip', $reserved_ips, 'reserved_ips');
}

unset($reserved_ips['blockedip_cachetime']);
if ($reserved_ips && !validip($ip, $reserved_ips))
{
	err('Invalid or reserved IP');
}

function validip($ip, $a=array())
{
	$ip=sprintf("%u", ip2long($ip));

	if ($ip && $a)
	{
		foreach ($a as $r)
		{
				$min = sprintf("%u", ip2long($r[0]));
				$max = sprintf("%u", ip2long($r[1]));
				if($ip >= $min && $ip <= $max)
				{
					return false;
				}
		}
		return true;
	}
	else
	{
		return true;
	}
}
?>
